package com.example.myapplication;

import static android.database.sqlite.SQLiteDatabase.OPEN_READWRITE;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {

    private EditText register_id;//id输入
    private EditText register_name;//姓名输入
    private EditText register_pwd;
    private RadioGroup radioGroup;
    private int radio;//存放选中radio的id
    private Button register_confirm;
    private Button register_back;

    //使用的属性
    private String id;
    private String name;
    private String pwd;
    MyDataBaseHelper helper_register;
    private String cr;//身份，用于标识将新数据插入哪个表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        //控件初始化
        register_id = findViewById(R.id.register_id);//id输入
        register_name = findViewById(R.id.register_name);//姓名输入
        register_pwd = findViewById(R.id.register_pwd);//密码输入
        radioGroup = findViewById(R.id.radioGroup2);//radio组
        register_confirm = findViewById(R.id.register_confirm);//注册按钮
        register_back = findViewById(R.id.register_back);//返回按钮


        //返回按钮，调用finish()返回上一页面
        register_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        register_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(register_id.getText().toString().trim().equals("") || register_name.getText().toString().trim().equals("")
                || register_pwd.getText().toString().trim().equals("")){
                    Toast.makeText(RegisterActivity.this,"输入不可为空",Toast.LENGTH_SHORT).show();
                } else if (!(register_id.getText().toString().length() == 6) || !(register_pwd.getText().length() == 6)
                || register_name.getText().toString().length() >6) {
                    Toast.makeText(RegisterActivity.this,"请检查输入长度",Toast.LENGTH_SHORT).show();
                } else{
                    //获得输入的各种属性
                    id = register_id.getText().toString().trim();
                    pwd = register_pwd.getText().toString().trim();
                    name = register_name.getText().toString().trim();
                    radio = radioGroup.getCheckedRadioButtonId();
                    if(radio == R.id.register_cr_stu){
                        cr = "stu";
                    } else if (radio == R.id.register_cr_tea) {
                        cr = "tea";
                    } else if (radio == R.id.register_cr_admin) {
                        cr = "admin";
                    }else{
                        finish();
                    }
                    Log.d("Register",id+" "+pwd+" "+name+"\n");
                    //尝试将新数据插入数据库
                    ContentValues values = new ContentValues();
                    try{
                        values.put("id",id);
                        values.put("name",name);
                        values.put("pwd",pwd);
                        SQLiteDatabase db = MyDataBaseHelper.getDatabase();
                        db.insert(cr,null,values);
                        Log.d("Register",id+" "+pwd+" "+name+"\n");
                        values.clear();
                        Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                        finish();
                    }catch (Exception e){
                        e.printStackTrace();
                        Toast.makeText(RegisterActivity.this,"创建失败，请检查输入",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}